package com.leilei.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leilei.commonutils.R;
import com.leilei.eduservice.client.VodClient;
import com.leilei.eduservice.entity.EduCourse;
import com.leilei.eduservice.entity.EduTeacher;
import com.leilei.eduservice.entity.vo.CourseInfoVo;
import com.leilei.eduservice.entity.vo.CoursePublishVo;
import com.leilei.eduservice.entity.vo.CourseQuery;
import com.leilei.eduservice.entity.vo.TeacherQuery;
import com.leilei.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-05-04
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {


    @Autowired
    private EduCourseService eduCourseService;


    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后的课程id，为了后面添加大纲使用
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }


    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }


    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }


    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }


    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");    //设置课程发布状态
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }


    //课程列表,基本实现
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);

    }


    //分页查询课程
    @GetMapping("pageCourse/{current}/{limit}")
    public R pageListCourse(@PathVariable long current,
                             @PathVariable long limit){

        //创建page对象
        Page<EduCourse> pageCourse = new Page<>(current,limit);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageCourse对象里面
        eduCourseService.page(pageCourse,null);

        long total = pageCourse.getTotal();    //总记录数
        List<EduCourse> records = pageCourse.getRecords();     //数据list集合

        Map map = new HashMap();
        map.put("total",total);
        map.put("rows",records);

        return R.ok().data(map);
        //return R.ok().data("total",total).data("rows",records);
    }




    //条件查询带分页的方法
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) CourseQuery courseQuery){

        //创建page对象
        Page<EduCourse> coursePage = new Page<>(current,limit);

        //构造条件
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper();

        //多条件组合查询
        //判断条件值是否为空，如果不为空拼接条件
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();

        if (!StringUtils.isEmpty(title)){
            //构建条件
            queryWrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            //构建条件
            queryWrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin)){
            //构建条件
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            //构建条件
            queryWrapper.le("gmt_create",end);
        }

        //排序
        queryWrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        eduCourseService.page(coursePage,queryWrapper);

        long total = coursePage.getTotal();    //总记录数
        List<EduCourse> records = coursePage.getRecords();     //数据list集合

        return R.ok().data("total",total).data("rows",records);

    }


    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }

}

